package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "orderGlobalFallbackMethod")
public class OrderHystrixController {
	@Autowired
	private PaymentHystrixService paymentHystrixService;

	@GetMapping("/hystrix/ok/{id}")
	public String paymentInfo_OK(@PathVariable("id") Integer id) {
		String result = paymentHystrixService.paymentInfo_OK(id);
		return result;
	}

	@GetMapping("/hystrix/timeout/{id}")
	/*@HystrixCommand(fallbackMethod ="paymentInfo_TIMEOUTHandler", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
	})*/
	@HystrixCommand
	public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
		String result = paymentHystrixService.paymentInfo_Timeout(id);
		return result;
	}

	public String paymentInfo_TIMEOUTHandler(@PathVariable("id") Integer id) {
		return "线程池： " + Thread.currentThread().getName() + " paymentInfo_TIMEOUT, 客户端服务降级";
	}

	/**
	 * 全局fallback方法
	 * @return
	 */
	public String orderGlobalFallbackMethod() {
		return "Global异常处理信息，请稍后在试";
	}
}
